{% extends "shop/base.html" %}
{% block title %}
    <title>商品分类-修改</title>
{% endblock %}

{% block content %}
    <div class="content-wrapper">
        <section class="content-header">
            <div class="container-fluid">
                <div class="row mb-2">
                    <div class="col-sm-12">
                        <ol class="breadcrumb float-sm-left">
                            <li class="breadcrumb-item"><a href="{{url_for('cates.edit',cat_id=cates.cat_id)}}">商品分类</a></li>
                            <li class="breadcrumb-item active">商品分类修改</li>
                        </ol>
                    </div>
                </div>
            </div>
        </section>
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <form novalidate method="post" class="form-horizontal"  enctype="multipart/form-data">
                        {{ form_obj.csrf_token }}
                        <div class="card card-primary">
                            <div class="card-header">
                                <h3 class="card-title">商品分类修改</h3>
                            </div>
                            <div class="card-body">
                                <div class="form-group row">
                                    <label for="inputtext"
                                           class="col-sm-2 col-form-label text-center">分类名称</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="name" name="name"
                                        value="{{ cate.name }}" placeholder="分类名称">
                                 <span style="color:red"></span>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="{{ form_obj.parent_id.id_for_label }}"
                                           class="col-sm-2 col-form-label text-center">选择分类</label>
                                    <div class="col-sm-10">
                                        <div class="select-list">
                                            <ul>
                                                <li>
                                                    <select id="parent_id" name="parent_id">
                                                        <option value=''>请选择...</option>
                                                        <option value='0'>作为一级分类</option>
                                                        {% for key,value in cates.items() %}
                                                            <option {% if cate.cat_id|string == key|string %}
                                                            selected
                                                            {% endif %}
                                                            value={{ key }}>{{ value|safe }}</option>
                                                        {% endfor %}
                                                    </select>
                                                    {#      {{ form_obj.parent_id }}#}
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="inputtext"
                                           class="col-sm-2 col-form-label text-center">排序</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" id="sort" name="sort"
                                        value="{{ cate.sort }}" placeholder="排序">
                                 <span style="color:red"></span>
                                    </div>
                                </div>
                                <div class="form-group row">
                                    <label for="inputtext"
                                           class="col-sm-2 col-form-label text-center">分类图片</label>
                                    <div class="col-sm-10">
                                        <div class="custom-file">
                                            {{ form_obj.logo }}
                                            <label class="custom-file-label" for="customFile">{%if cate.logo %}{{ cate.logo }}{% else %}选择图片{% endif %}
                                            </label>
                                        </div>
                                        {% if cate.logo %}
                                                <img id="preview-image" src="/cates/show?filename={{ cate.logo }}"
                                                     style="width: 150px; height: 150px;"/>
                                        {% else %}
                                                <img id="preview-image" src="{{ url_for('static',filename='img/default-150x150.png')}}"
                                                     style="width: 150px; height: 150px;"/>
                                        {% endif %}
                                    </div>
                                </div>
                                <div class="card-footer text-center">
                                        <button type="submit" class="btn btn-info">保存</button>
                                        <button type="button" class="btn btn-default"
                                                onclick="javascript:window.location='{{ url_for("cates.index") }}'">返回
                                        </button>
                                    {{ info }}
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </section>
    </div>
<script>
document.addEventListener("DOMContentLoaded", function() {
        const logoInput = document.querySelector("input[name='{{ form_obj.logo.name }}']");
        const logoPreview = document.getElementById("preview-image");
        const customFileLabel = document.querySelector(".custom-file-label");

        logoInput.addEventListener("change", function() {
            const file = this.files[0];
            if (file) {
                const reader = new FileReader();
                reader.onload = function(e) {
                    logoPreview.src = e.target.result;
                    // 更新文件标签显示的文件名
                    customFileLabel.textContent = file.name;
                }
                reader.readAsDataURL(file);
            } else {
                logoPreview.src = "#";
                customFileLabel.textContent = "选择图片";
            }
        });
    });
</script>
{% endblock %}